package com.carryking.smart.rpc.netty.code;

import com.carryking.smart.rpc.entity.RequestEntity;
import com.carryking.smart.rpc.utils.KryoUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by jincarry on 2018/1/10.
 */
@Slf4j
public class RequestEntityEncoder extends MessageToByteEncoder<RequestEntity> {


    protected void encode(ChannelHandlerContext ctx, RequestEntity msg, ByteBuf out) throws Exception {
        if (msg == null) {
            log.error("Encoder RequestEntity失败,远程服务调用请求实体不能为空");
            throw new RuntimeException("远程服务调用请求实体不能为空");
        }
        if (log.isDebugEnabled()) {
            log.debug("Encoder RequestEntity:RequestEntity={}",msg);
        }
        out.writeBytes(KryoUtils.wirte(msg));
    }
}
